package com.conjane.algo.linked_list;

public class ListNode<E> {
    E val;
    ListNode<E> next;
    ListNode(){}
    ListNode(E val){
        this.val = val;
    }
    ListNode(E val, ListNode<E> next){
        this.val = val;
        this.next = next;
    }

    void add(E e){
        ListNode<E> node = new ListNode<>(e);
        if (this.next == null){
            this.next = node;
        }else {
            ListNode<E> temp = this;
            while (temp.next != null){
                temp = temp.next;
            }
            temp.next = node;
        }
    }

    @Override
    public String toString() {
        ListNode<E> temp = this;
        StringBuilder builder = new StringBuilder();
        while (temp.next != null){
            temp = temp.next;
            builder.append(temp.val).append("->");
        }
        builder.append("null");
        return builder.toString();
    }
}
